package com.hbnu.controller;

import com.hbnu.service.ReceiveThread;
import com.hbnu.service.SendThread;

import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            String serverIp = JOptionPane.showInputDialog("Enter Server IP:", "127.0.0.1");
            String portStr = JOptionPane.showInputDialog("Enter Server Port:", "12345");
            int serverPort = Integer.parseInt(portStr);
            socket = new Socket(serverIp, serverPort);
            BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
            PrintWriter out =new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8),true);
            Scanner scanner = new Scanner(new InputStreamReader(System.in, StandardCharsets.UTF_8));
            boolean flag=true;
            while (flag) {
                System.out.println(in.readLine());
                System.out.println(in.readLine());
                System.out.println(in.readLine());
                int number= Integer.parseInt(scanner.nextLine());
                out.println(number);
                switch (number){
                    case 1:
                        System.out.println(in.readLine());
                        out.println(scanner.nextLine());
                        out.println(scanner.nextLine());
                        String rslt=in.readLine();
                        System.out.println(rslt);
                        if (rslt.equals("登录成功！")) {
                            flag=false;
                        }
                        in.readLine();
                        break;
                    case 2:
                        System.out.println(in.readLine());
                        out.println(scanner.nextLine());
                        String nameInformation=in.readLine();
                        System.out.println(nameInformation);
                        if (nameInformation.equals("请输入您的密码:")) {
                            out.println(scanner.nextLine());
                            String phoneInformation=in.readLine();
                            System.out.println(phoneInformation);
                            if (phoneInformation.equals("请输入您的电话号码:")) {
                                out.println(scanner.nextLine());
                                String registersInformation=in.readLine();
                                System.out.println(registersInformation);
                                if (registersInformation.equals("请输入注册码：")) {
                                    int count=0;
                                    while (count++<3) {
                                        out.println(scanner.nextLine());
                                        String createInformation=in.readLine();
                                        System.out.println(createInformation);
                                        if (createInformation.equals("创建成功")||createInformation.equals("创建失败")) {
                                            break;
                                        }
                                    }
                                }
                            }
                        }else {
                            in.readLine();
                        }
                        break;
                    case 3:
                        System.out.println(in.readLine());
                        out.println(scanner.nextLine());
                        String phoneCondition=in.readLine();
                        System.out.println(phoneCondition);
                        if (phoneCondition.equals("请输入验证码：")) {
                            int count=0;
                            while (count++<3) {
                                out.println(scanner.nextLine());
                                String createInformation = in.readLine();
                                System.out.println(createInformation);
                                if (createInformation.equals("修改成功！") || createInformation.equals("修改失败！")) {
                                    System.out.println(in.readLine());
                                    break;
                                }
                            }
                        }
                        break;
                    case 4:
                        System.exit(0);
                    default:
                        System.out.println(in.readLine());
                    }
            }
            new Thread(new SendThread(socket)).start();
            Thread receiveThread = new Thread(new ReceiveThread(socket));
            receiveThread.start();
            receiveThread.join();
        } catch (Exception e) {
            System.out.println("服务器异常");
            System.exit(0);
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
            }catch (IOException e) {
                System.out.println("关闭异常");
            }

        }
    }

}
